var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});

gulp.task('server', ["sassfile"], function() {
    browserSync.init({
        files: ['./**/**.html', './**/**.js', './**/**.css'],
        server: {
            baseDir: "./public",
            middleware: function(req, res, next) {
                if (req.url.indexOf("/api/") != -1) {
                    proxy.web(req, res, { target: 'http://localhost:9000/' });
                } else {
                    next();
                }
            }
        },

        notify: false
    });
    gulp.watch('./public/scss/**/**.scss', ['sassfile']);
});


gulp.task('sassfile', function() {
    return gulp.src('./public/scss/**/style.scss').pipe(sass()).pipe(gulp.dest('./public/dist'));
});

gulp.task('default', ['server']);
gulp.task('complie', ['sassfile']);